<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <h2>{{id?'编辑':'新增'}}评论</h2>
    </div>
    <el-form :label-position="labelPosition" label-width="100px" @submit.native.prevent="saveComment">
      <el-form-item label="评论用户：" style="margin-left: 20%">
        <el-select v-model="model.user" placeholder="请选择">
          <el-option
            v-for="item in userinfo"
            :key="item._id"
            :label="item.nickname"
            :value="item._id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文章列表：" style="margin-left: 20%">
        <el-select v-model="model.articles" placeholder="请选择">
          <el-option
            v-for="item in articlesData"
            :key="item._id"
            :label="item.title"
            :value="item._id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="评论内容：" style="margin-left: 20%;width: 500px;">
        <el-input
          type="textarea"
          :autosize="{ minRows: 3, maxRows: 5}"
          placeholder="请输入内容"
          v-model="model.content">
        </el-input>
      </el-form-item>
      <el-form-item style="margin-left: 35%">
        <el-button type="success" round  native-type="submit">提交</el-button>
      </el-form-item>

    </el-form>
  </el-card>
</template>

<script>
  import auth from '../../mixin/index'
  export default {
    name: "AdminUserEdit",
    props:['id'],
    mixins:[auth],
    data(){
      return {
        labelPosition: 'right',
        model:{
          user:'',
          content:'',
          articles:''
        },
        userinfo:{},
        articlesData:{}
      }
    },
    methods:{
      // 保存
      async saveComment(){
        if(this.id){
          await this.$http.put(`/rest/comments/${this.id}`,this.model)
          this.$router.push('/comments/list')
          this.$message({
            message: `修改评论成功`,
            type: 'success'
          });
        }else {
          await this.$http.post('/rest/comments',this.model)
          this.$router.push('/comments/list')
          this.$message({
            message: `添加评论成功`,
            type: 'success'
          });
        }
      },
      // 获取用户信息
      async getUserInfo(){
        const res = await this.$http.get('/rest/users')
        console.log(res)
        this.userinfo = res.data.list
      },
      // 获取文章信息
      async getArticlesData(){
        const res = await this.$http.get('/rest/articles')
        this.articlesData = res.data.list
      } ,
      // 获取评论信息
      async getCommentEditData(){
        const res =  await this.$http.get(`/rest/comment/${this.id}`)
        this.model = res.data
      },

    },
    created() {
      this.getUserInfo()
      this.getArticlesData()
      this.id && this.getCommentEditData()
    }
  }
</script>

<style lang="scss" scoped>

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border: 1px dashed #d9d9d9;
    &:hover{
      border-color: #409EFF;
    }
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
